package win.bigwhale.sudoku.game;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

import win.bigwhale.sudoku.SudokuUtil;

public class Wardrobe {
	private static Wardrobe wardrobe = new Wardrobe();
	private ArrayList<Sudoku> easyGameList = new ArrayList<>();
	private ArrayList<Sudoku> mediumGameList = new ArrayList<>();
	private ArrayList<Sudoku> hardGameList = new ArrayList<>();
	private ArrayList<Sudoku> impossibleGameList = new ArrayList<>();

	private Wardrobe() {
		initGameLists();
	}

	public static Wardrobe getWardrobe() {
		return wardrobe;
	}

	private void initGameLists() {
		initGameList(SudokuUtil.EASY_GRADE_GAME_FILE, easyGameList, 0);
		initGameList(SudokuUtil.MEDIUM_GRADE_GAME_FILE, mediumGameList, 1);
		initGameList(SudokuUtil.HARD_GRADE_GAME_FILE, hardGameList, 2);
		initGameList(SudokuUtil.IMPOSSIBLE_GRADE_GAME_FILE, impossibleGameList, 3);
	}

	private void initGameList(String gradeGameFilePath, ArrayList<Sudoku> gameList, int grade) {
		File gradeGameFile = new File(gradeGameFilePath);
		Scanner inputGame = null;
		try {
			inputGame = new Scanner(gradeGameFile);
			while (inputGame.hasNextLine()) {
				Sudoku sudoku = new Sudoku(inputGame.nextLine(), grade);
				gameList.add(sudoku);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (inputGame != null) {
				inputGame.close();
			}
		}
	}

	public Sudoku getRandomGame() {
		ArrayList<Sudoku> randomGrade = getRandomGrade();
		return randomGrade.get((int)(Math.random() * randomGrade.size()));
	}

	private ArrayList<Sudoku> getRandomGrade() {
		switch ((int)(Math.random() * 4)) {
		case 0:
			return easyGameList;
		case 1:
			return mediumGameList;
		case 2:
			return hardGameList;
		default:
			return impossibleGameList;
		}
	}

	public Sudoku getRandomEasyGame() {
		return easyGameList.get((int)(Math.random() * easyGameList.size()));
	}

	public Sudoku getRandomMediumGame() {
		return mediumGameList.get((int)(Math.random() * mediumGameList.size()));
	}

	public Sudoku getRandomHardGame() {
		return hardGameList.get((int)(Math.random() * hardGameList.size()));
	}

	public Sudoku getRandomImpossibleGame() {
		return impossibleGameList.get((int)(Math.random() * impossibleGameList.size()));
	}
}
